﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DotNet.WebUI.Admin
{
    public partial class UsersEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Operate"] = GetQueryString("Operate", "New");
                ViewState["LoginID"] = GetQueryString("LoginID", "");

                if (!string.IsNullOrEmpty(ViewState["LoginID"].ToString()))
                {
                    LoadUsers();
                }
            }
        }

        private void LoadUsers()
        {
            string sql = @"SELECT *
                FROM Users
                WHERE LoginID = '{0}' ";
            sql = string.Format(sql, ViewState["LoginID"].ToString());

            DataRow objRow = ExecuteDataTable(sql).Rows[0];

            txtLoginID.ReadOnly = txtPwd.ReadOnly = txtConPwd.ReadOnly = true;
            txtPwd.Text = txtConPwd.Text = "************";
            txtLoginID.Text = objRow["LoginID"].ToString();
            txtUserName.Text = objRow["UserName"].ToString();
            txtDepict.Text = objRow["Depict"].ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtConPwd.Text.Trim() != txtPwd.Text.Trim())
                {
                    MessageBox("密码和确认密码不相符合，请重新输入密码！");
                    return;
                }

                string _Sql = "";

                if (ViewState["Operate"].ToString().ToLower() == "new")
                {
                    if ((int)ExecuteScalar(string.Format(@"SELECT Count(*) 
                        FROM Users
                        WHERE LoginID = '{0}' ",
                      txtLoginID.Text)) >= 1)
                    {
                        MessageBox("登录代码已经存在，请重新输入！");
                        return;
                    }

                    _Sql = @"INSERT INTO Users(LoginID, Pwd, UserName, Depict)  
                        VALUES('{0}', '{1}', '{2}', '{3}')";
                    _Sql = string.Format(_Sql,
                        txtLoginID.Text,
                        txtPwd.Text,
                        txtUserName.Text,
                        txtDepict.Text);
                }
                else if (ViewState["Operate"].ToString().ToLower() == "edit")
                {
                    _Sql = @"UPDATE Users
                        SET UserName = '{1}',
                            Depict = '{2}'
                        WHERE LoginID = '{0}' ";

                    _Sql = string.Format(_Sql,
                        ViewState["LoginID"].ToString(),
                        txtUserName.Text,
                        txtDepict.Text);
                }

                if (ExecuteNonQuery(_Sql) >= 1)
                {
                    MessageBox("数据保存成功!");
                }
            }
            catch (Exception ex)
            {
                MessageBox(ex.Message);
            }
        }
    }
}